/**
    File        : Options.cpp
    Description : Global application settings
    Author      : Raymond Marsh
    Created     : 08/01/2009
    Copyright 2009, 2010, 2011, 2012 Raymond Marsh

    This program is distributed under the terms of the GNU General Public License.
    To view a copy of the license see the file 'copying.txt'

    This file is part of the MyMoney Suite.

    MyMoney is free software: you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free Software Foundation, either
    version 3 of the License, or (at your option) any later version.

    MyMoney is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with MyMoney.
    If not, see <http://www.gnu.org/licenses/>.
**/

#include "..\include\Options.h"

/* List of key / values
    GLOBAL.FirstRun                bool            Is this the first time the program has been run?
    GLOBAL.Language                int             Language to display text in
    COLORS.MainBGColor             wxColour        Top level wxFrame backgound color
    COLORS.DialogBGColor           wxColor         All dialog window background color
    FONTSIZES.RegisterFontSize     int             Font size for all registers
    WARNINGS.UseCategory           bool            Has a category been entered in a transaction
    LOGIN.DefaulUserName           wxString        default user name
    LOGIN.DefaultPassword          wxString        default password
    MYSQL.UseLocalConnection       bool            connect to MySQL using localhost vs. remote connection
*/

namespace mm
{
COptions* g_pOptions;

bool COptions::Initialize()
{
    // defaults
    m_DefaultMainBGColor.Set(27, 137, 40);
    m_DefaultDialogBGColor.Set(27, 137, 40);
    m_DefaultHideReconciledTransactionsOnStart = true;
    m_DefaultLanguage = 0;
    m_DefaultRegisterFontSize = 12;

    GLOBAL.FirstRun                          = false;
    GLOBAL.HideReconciledTransactionsOnStart = m_DefaultHideReconciledTransactionsOnStart;
    GLOBAL.Language                          = m_DefaultLanguage;
    COLORS.MainBGColor                       = m_DefaultMainBGColor;
    COLORS.DialogBGColor                     = m_DefaultDialogBGColor;
    FONTSIZES.RegisterFontSize               = m_DefaultRegisterFontSize;
    LOGIN.DefaultUserName                    = wxEmptyString;
    LOGIN.DefaultPassword                    = wxEmptyString;
    MYSQL.UseLocalConnection                 = true;
    WARNINGS.UseCategory                     = true;
    return true;
}

bool COptions::LoadOptions()
{
    if (!wxFile::Exists(mmOptionsFile) )
    {
        wxFileOutputStream FOS(mmOptionsFile);
        FOS.Close();
    }

    wxString str;

    wxFileInputStream FIS(mmOptionsFile);
    wxFileConfig *pConfig = new wxFileConfig(FIS);
    wxConfigBase::Set(pConfig);
    pConfig->DontCreateOnDemand();
    pConfig->Read("/GLOBAL/FirstRun", &GLOBAL.FirstRun, true);
    pConfig->Read("/GLOBAL/HideReconciledTransactionsOnStart", &GLOBAL.HideReconciledTransactionsOnStart);
    pConfig->Read("/GLOBAL/Language", &GLOBAL.Language, 0);
    if (pConfig->Read("/COLORS/MainBGColor", &str) )
    {
        wxFromString(str, &COLORS.MainBGColor);
    }
    if (pConfig->Read("/COLORS/DialogBGColor", &str) )
    {
        wxFromString(str, &COLORS.DialogBGColor);
    }
    pConfig->Read("/FONTSIZES/RegisterFontSize", &FONTSIZES.RegisterFontSize, m_DefaultRegisterFontSize);
    pConfig->Read("/LOGIN/DefaultUserName", &LOGIN.DefaultUserName, wxEmptyString);
    pConfig->Read("/LOGIN/DefaultPassword", &LOGIN.DefaultPassword, wxEmptyString);
    pConfig->Read("/MYSQL/UseLocalConnection", &MYSQL.UseLocalConnection, true);
    pConfig->Read("/WARNINGS/UseCategory", &WARNINGS.UseCategory, true);

    return true;
}

bool COptions::SaveOptions()
{
    wxFileStream FOS(mmOptionsFile);
    wxFileConfig *pConfig = (wxFileConfig*)wxConfigBase::Get();

    pConfig->Write("/GLOBAL/FirstRun", GLOBAL.FirstRun);
    pConfig->Write("/GLOBAL/HideReconciledTransactionsOnStart", GLOBAL.HideReconciledTransactionsOnStart);
    pConfig->Write("/GLOBAL/Language", GLOBAL.Language);
    pConfig->Write("/COLORS/MainBGColor", wxToString(COLORS.MainBGColor) );
    pConfig->Write("/COLORS/DialogBGColor", wxToString(COLORS.DialogBGColor) );
    pConfig->Write("/FONTSIZES/RegisterFontSize", FONTSIZES.RegisterFontSize);
    pConfig->Write("/LOGIN/DefaultUserName", LOGIN.DefaultUserName);
    pConfig->Write("/LOGIN/DefaultPassword", LOGIN.DefaultPassword);
    pConfig->Write("/WARNINGS/UseCategory", WARNINGS.UseCategory);

    pConfig->Save(FOS);
    return true;

}

bool COptions::RestoreDefaults()
{
    GLOBAL.HideReconciledTransactionsOnStart = m_DefaultHideReconciledTransactionsOnStart;
    GLOBAL.Language = m_DefaultLanguage;
    COLORS.MainBGColor = m_DefaultMainBGColor;
    COLORS.DialogBGColor = m_DefaultDialogBGColor;
    FONTSIZES.RegisterFontSize = m_DefaultRegisterFontSize;
    LOGIN.DefaultUserName = wxEmptyString;
    LOGIN.DefaultPassword = wxEmptyString;
    MYSQL.UseLocalConnection = true;
    WARNINGS.UseCategory = true;

    return true;
}


}; // end namespace mm

